#!/usr/bin/ruby

class Episode
  REGEXP_COLLECTION=[
        {:regexp=>/([A-z0-9\s]+)S([0-9]{2})E([0-9]{2})/,
          :fields=>{
          :series_name=>1,
          :episode=>2, 
          :season=>3},
          :numbers=>[:episode, :season]
        }
      ]

  def valid; !!@match; end

  def initialize(filename)
    @filename=filename
    @match=REGEXP_COLLECTION.detect do |e|
      if e[:regexp].match(filename)
        e
      end
    end
  end

  def method_missing( name, *args )
    if @match[:fields][name.to_sym]
      matched(name.to_sym)
    else
      nil
    end
  end

private

  def matched(n)
    v=@match[:regexp].match(@filename)[@match[:fields][n]]
    if @match[:numbers].include? n
      v=v.to_i
    end
    return v
  end

end

e=Episode.new('Stargate Atlantis S02E03')
puts e.series_name
puts e.episode
puts e.season
